﻿using System;
using System.IO;
using ReportFramework.Models;
using System.Collections.Generic;

namespace ReportFramework.Common
{
    public class IOHelper
    {
        /// <summary>
        /// look up directory, get files comments to list<>
        /// </summary>
        /// <param name="info"></param>
        /// <param name="reportTemplatesCommentsList"></param>
        /// <param name="serialNum"></param>
        /// <param name="pId"></param>
        public static void ReadReportTemplatesCommentsToList(FileSystemInfo info, ref List<ReportTemplatesCommentsModel> reportTemplatesCommentsList, ref int serialNum, int? pId)
        {
            if (!info.Exists) return;
            var dir = info as DirectoryInfo;
            if (dir == null) return;
            var thisPId = serialNum;
            reportTemplatesCommentsList.Add(new ReportTemplatesCommentsModel
            {
                Id = thisPId,
                ParentId = pId,
                Title = dir.Name == "templates" ? "所有报表" : dir.Name,
                Url = "Home/Report" + dir.FullName.Substring(dir.FullName.IndexOf("templates") + 9).Replace(@"\", "/"),
                Proposer = "",
                DateOfPresentation = "",
                Description = ""
            });

            var files = dir.GetFileSystemInfos();
            foreach (var t in files)
            {
                var file = t as FileInfo;
                if (file != null)
                {
                    if (file.Extension == "")
                    {
                        serialNum++;
                        var commentsDic = GetReportTemplateFileComments(file);
                        if (!commentsDic.ContainsKey("状态") || commentsDic["状态"].Trim() == "" ||
                            commentsDic["状态"].ToLower().Trim() == "enable")
                        {
                            reportTemplatesCommentsList.Add(new ReportTemplatesCommentsModel
                            {
                                Id = serialNum,
                                ParentId = thisPId,
                                Title = file.Name,
                                Url = "Home/Report" + file.FullName.Substring(file.FullName.IndexOf("templates") + 9).Replace(@"\", "/"),
                                Proposer = commentsDic["提出者"],
                                DateOfPresentation = commentsDic["提出日期"],
                                Description = commentsDic["描述"]
                            });
                        }
                    }
                }
                else
                {
                    serialNum++;
                    ReadReportTemplatesCommentsToList(t, ref reportTemplatesCommentsList, ref serialNum, thisPId);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Dictionary<string,string> GetReportTemplateFileComments(FileInfo file)
        {
            var dicObj = new Dictionary<string, string>();
            using (var sr = file.OpenText())
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    if (s.StartsWith("//"))
                    {
                        var keyValue=s.Replace("//", "").Split(':');
                        try
                        {
                            dicObj.Add(keyValue[0], keyValue[1]);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        break;
                    }
                    Console.WriteLine(s);
                }
            }
            return dicObj;
        }
    }
}